package edu.washington.cs.workflow.analysis.guistructure.uievents;

import org.uispec4j.Panel;
import org.uispec4j.RadioButton;
import org.uispec4j.UIComponent;

import edu.washington.cs.workflow.analysis.guistructure.uievents.UIEvent.WINDOW;
import edu.washington.cs.workflow.util.Utils;

public class RadioButtonEvent extends AbstractUIEvent  {

	private final RadioButton radioButton;
	private final UIAction action;
	
	public RadioButtonEvent(RadioButton radio, Panel panel, UIAction action) {
		Utils.checkNotNull(radio);
		Utils.checkNotNull(panel);
		Utils.checkNotNull(action);
		Utils.checkTrue(action.equals(UIAction.Click), "illegal action: " + action);
		this.radioButton = radio;
		this.setCurrentPanel(panel);
		this.action = action;
	}
	
	@Override
	public UIComponent getUIComponent() {
		return this.radioButton;
	}

	@Override
	public UIAction getAction() {
		return this.action;
	}

	@Override
	public String toString() {
		return this.action + " radiobutton " + this.radioButton.getLabel();
	}
	
	@Override
	protected WINDOW probeWindowInfoInternal() {
		return WINDOW.NONE;
	}

	@Override
	public boolean canFireEvent() {
		return this.radioButton.isEnabled().isTrue();
	}
	
	@Override
	protected UIEventOutcome fireEventInternal() {
		
		UIEventOutcome outcome = new UIEventOutcome();
		Throwable error = null;
		try {
			this.radioButton.click();
		}  catch (Throwable e) {
			error = e;
		}
		
		if(error != null) {
		    outcome.setError(error);
		}
		outcome.currentPanelClosed(super.isCurrentPanelClosed());
		return outcome;
	}

}
